package com.util;

import com.qiniu.common.Config;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PosterUtil {

    /***
     * @description:生成推广二维码
     *
     * @author Asan
     * @date 2019-09-05
     *  * @param
     */
    public static String payQrPoster() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳

        String filename = DigestUtils.md5Hex(date) + ".jpg";
        String key = "payqrposter/" + filename;
        String imgUrl = SystemConstant.DOMAIN_IMAGE + "/" + key;
        long time = (new Date()).getTime();
        if (StringUtils.isNotBlank(HttpRequest.sendGet(imgUrl, "qhash/md5&v=" + time))) {
            return imgUrl + "?v=" + time;
        }
        //读取图片文件，得到BufferedImage对象
        BufferedImage bimg;
        try {
            String qrurl=String.format(SystemConstant.QR_URL);
            BufferedImage QRImage=QRCodeUtil.createQRCode(qrurl,536,536);
            bimg = ImageIO.read(new FileInputStream(""));
            //得到Graphics2D 对象
            Graphics2D g2d=(Graphics2D)bimg.getGraphics();
            //绘制二维码
            g2d.drawImage(QRImage,276,276,null);
            // 释放对象
            g2d.dispose();
            File file =new File(SystemConstant.TEMP_PATH+filename);
            File parent = file.getParentFile();
            if(!parent.exists()){
                parent.mkdirs();
            }
            //保存新图片
            ImageIO.write(bimg, "JPG",file);
            Auth auth = Auth.create(Config.ACCESS_KEY, Config.SECRET_KEY);
            Configuration cfg = new Configuration(Region.region0());
            UploadManager uploadManager = new UploadManager(cfg);
            uploadManager.put(file, key, auth.uploadToken("image", key));
            file.delete();
            return imgUrl+"?v="+time;
        } catch (Exception e) {

        }
        return null;
    }
}
